var express = require('express');
var router = express.Router();
var axios = require('axios').default;

/* GET home page. */
router.get('/', function(req, res, next) {
  res.render('index', { title: 'Express' });
});
router.get('/prefer', async function(req, res, next) {
  // let data = await req.authing.login({email: 'test1@123.com', password: '123456'})
  // console.log(data)
  console.log(req.headers);
  let token = req.headers.authorization;
  token = token.replace('Bearer ', '');
  try {
    let data = await req.authing.decodeToken(token);
    let p1 = axios.get('http://mock.yelexin.cn/areaprefers?authing_id=' + data.data.id);
    let p2 = axios.get('http://mock.yelexin.cn/priceprefers?authing_id=' + data.data.id);
    let p3 = axios.get('http://mock.yelexin.cn/regionprefers?authing_id=' + data.data.id);
    let [r1, r2, r3] = await Promise.all([p1, p2, p3]);

    let likeArea = [
      { key: 'p0_50', count: r1.data[0].p0_50 },
      { key: 'p50_70', count: r1.data[0].p50_70 },
      { key: 'p70_90', count: r1.data[0].p70_90 },
      { key: 'p90_1000', count: r1.data[0].p90_1000 }
    ];
    likeArea.sort((a, b) => a.count - b.count);
    let likePrice = [
      { key: 'p0_200', count: r2.data[0].p0_200 },
      { key: 'p200_300', count: r2.data[0].p200_300 },
      { key: 'p300_400', count: r2.data[0].p300_400 },
      { key: 'p400_500', count: r2.data[0].p400_500 },
      { key: 'p500_600', count: r2.data[0].p500_600 },
      { key: 'p600_10000', count: r2.data[0].p600_10000 }
    ];
    likePrice.sort((a, b) => a.count - b.count);
    let likeRegion = [
      { key: 'jiading', count: r3.data[0].jiading },
      { key: 'jinshan', count: r3.data[0].jinshan },
      { key: 'baoshan', count: r3.data[0].baoshan },
      { key: 'pudong', count: r3.data[0].pudong },
      { key: 'yangpu', count: r3.data[0].yangpu },
      { key: 'hongkou', count: r3.data[0].hongkou },
      { key: 'jingan', count: r3.data[0].jingan },
      { key: 'changning', count: r3.data[0].changning },
      { key: 'huangpu', count: r3.data[0].huangpu },
      { key: 'xuhui', count: r3.data[0].xuhui },
      { key: 'minxing', count: r3.data[0].minxing },
      { key: 'qingpu', count: r3.data[0].qingpu },
      { key: 'songjiang', count: r3.data[0].songjiang },
      { key: 'fengxian', count: r3.data[0].fengxian }
    ];
    likeRegion.sort((a, b) => a.count - b.count);
    let regionMap = {
      chongming: '崇明区',
      jiading: '嘉定区',
      jinshan: '金山区',
      baoshan: '宝山区',
      pudong: '浦东新区',
      yangpu: '杨浦区',
      hongkou: '虹口区',
      jingan: '静安区',
      putuo: '普陀区',
      changning: '长宁区',
      huangpu: '黄浦区',
      xuhui: '徐汇区',
      minxing: '闵行区',
      qingpu: '青浦区',
      songjiang: '松江区',
      fengxian: '奉贤区'
    };
    let bestArea = likeArea[likeArea.length - 1];
    let bestPrice = likePrice[likePrice.length - 1];
    let bestRegion = likeRegion[likeRegion.length - 1];
    console.log(bestArea, bestPrice, bestRegion)
    let rec = await axios.get('http://mock.yelexin.cn/houses', {
      params: {
        area_gte: bestArea.key.slice(1).split('_')[0],
        area_lt: bestArea.key.slice(1).split('_')[1],
        price_gte: bestPrice.key.slice(1).split('_')[0],
        price_lt: bestPrice.key.slice(1).split('_')[1],
        block: regionMap[bestRegion.count]
      }
    });
    if(rec.data.length === 0) {
      let rand = await axios.get('http://mock.yelexin.cn/houses');
      for(let i=0;i<rand.data.length;i++) {
        let num = parseInt(Math.random() * rand.data.length)
        let t = rand.data[i]
        rand.data[i] = rand.data[num]
        rand.data[num] = t
      }
      res.json(rand.data.slice(0, 4))
    } else {
      let r = parseInt(Math.random() * rec.data.length)-4
      res.json(rec.data.slice(r, r+4))
    }
  } catch (err) {
    console.log(err);
    res.json({ err: 1 });

  }
});
router.get('/save_prefer', async function(req, res, next) {
  // let data = await req.authing.login({email: 'test1@123.com', password: '123456'})
  // console.log(data)
  let token = req.headers.authorization;
  // 收集点击信息
  let { price, area, block, id } = req.query;
  console.log('block5782346');
  console.log(block);
  token = token.replace('Bearer ', '');
  try {
    let data = await req.authing.decodeToken(token);
    let authing_id = data.data.id;
    let userPricePrefer = await axios.get('http://mock.yelexin.cn/priceprefers', {
      params: {
        authing_id
      }
    });
    if (userPricePrefer.data.length) {
      console.log('userPricePrefer.data1111231241234');
      console.log(userPricePrefer.data);
      // 用户价格偏好
      userPricePrefer = userPricePrefer.data[0];
      let updated;
      if (price < 50) {
        updated = await axios.put('http://mock.yelexin.cn/priceprefers/' + userPricePrefer.id, {
          p0_200: userPricePrefer.p0_200 + 1
        });
      } else if (price >= 200 && price < 300) {
        updated = await axios.put('http://mock.yelexin.cn/priceprefers/' + userPricePrefer.id, {
          p200_300: userPricePrefer.p200_300 + 1
        });
      } else if (price >= 300 && price < 400) {
        updated = await axios.put('http://mock.yelexin.cn/priceprefers/' + userPricePrefer.id, {
          p300_400: userPricePrefer.p300_400 + 1
        });
      } else if (price >= 400 && price < 500) {
        updated = await axios.put('http://mock.yelexin.cn/priceprefers/' + userPricePrefer.id, {
          p400_500: userPricePrefer.p400_500 + 1
        });
      } else if (price >= 500 && price < 600) {
        updated = await axios.put('http://mock.yelexin.cn/priceprefers/' + userPricePrefer.id, {
          p500_600: userPricePrefer.p500_600 + 1
        });
      } else {
        updated = await axios.put('http://mock.yelexin.cn/priceprefers/' + userPricePrefer.id, {
          p600_10000: userPricePrefer.p600_10000 + 1
        });
      }
    } else {
      updated = await axios.post('http://mock.yelexin.cn/priceprefers', {
        authing_id,
        p0_200: 0,
        p200_300: 0,
        p300_400: 0,
        p400_500: 0,
        p500_600: 0,
        p600_10000: 0
      });
    }

    // 面积偏好
    let userAreaPrefer = await axios.get('http://mock.yelexin.cn/areaprefers', {
      params: {
        authing_id
      }
    });
    if (userAreaPrefer.data.length) {
      userAreaPrefer = userAreaPrefer.data[0];
      if (area < 50) {
        updated = await axios.put('http://mock.yelexin.cn/areaprefers/' + userAreaPrefer.id, {
          p0_50: userAreaPrefer.p0_50 + 1
        });
      } else if (area >= 50 && area < 70) {
        updated = await axios.put('http://mock.yelexin.cn/areaprefers/' + userAreaPrefer.id, {
          p50_70: userAreaPrefer.p50_70 + 1
        });
      } else if (area >= 70 && area < 90) {
        updated = await axios.put('http://mock.yelexin.cn/areaprefers/' + userAreaPrefer.id, {
          p70_90: userAreaPrefer.p70_90 + 1
        });
      } else {
        updated = await axios.put('http://mock.yelexin.cn/areaprefers/' + userAreaPrefer.id, {
          p90_1000: userAreaPrefer.p90_1000 + 1
        });
      }
    } else {
      updated = await axios.post('http://mock.yelexin.cn/areaprefers', {
        authing_id,
        p0_50: 0,
        p50_70: 0,
        p70_90: 0,
        p90_1000: 0
      });
    }

    // 区域偏好
    let blockMap = {
      崇明区: 'chongming',
      嘉定区: 'jiading',
      金山区: 'jinshan',
      宝山区: 'baoshan',
      浦东新区: 'pudong',
      杨浦区: 'yangpu',
      虹口区: 'hongkou',
      静安区: 'jingan',
      普陀区: 'putuo',
      长宁区: 'changning',
      黄浦区: 'huangpu',
      徐汇区: 'xuhui',
      闵行区: 'minxing',
      青浦区: 'qingpu',
      松江区: 'songjiang',
      奉贤区: 'fengxian'
    };
    let userRegionPrefer = await axios.get('http://mock.yelexin.cn/regionprefers', {
      params: {
        authing_id
      }
    });
    if (userRegionPrefer.data.length) {
      userRegionPrefer = userRegionPrefer.data[0];
      let res = await axios.put('http://mock.yelexin.cn/regionprefers/' + userRegionPrefer.id, {
        [blockMap[block]]: userRegionPrefer[blockMap[block]] + 1
      });
    } else {
      let res = await axios.post('http://mock.yelexin.cn/regionprefers', {
        authing_id,
        chongming: 0,
        jiading: 0,
        jinshan: 0,
        baoshan: 0,
        pudong: 0,
        yangpu: 0,
        hongkou: 0,
        jingan: 0,
        putuo: 0,
        changning: 0,
        huangpu: 0,
        xuhui: 0,
        minxing: 0,
        qingpu: 0,
        songjiang: 0,
        fengxian: 0
      });
    }
  } catch (err) {
    console.log(err);
  }
  res.json({ ok: 1 });
});
module.exports = router;
